//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package bulletin_dao

import (
	"context"
	"fmt"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type UnReadInfoDbItem struct {
	UnReadId     string `bson:"_id"`
	ProjectId    string `bson:"projectId"`
	MemberUserId string `bson:"memberUserId"`
	BulletinId   string `bson:"bulletinId"`
	TimeStamp    int64  `bson:"timeStamp"`
}

type _UnReadInfoDao struct {
	mc *mongo.Collection
}

func newUnReadInfoDao(db *mongo.Database) (*_UnReadInfoDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT_BULLETIN + "unread")
	_, err := mc.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
		{
			Keys: bson.D{{"projectId", 1}, {"memberUserId", 1}, {"timeStamp", 1}},
		},
		{
			Keys: bson.D{{"projectId", 1}, {"bulletinId", 1}},
		},
	})
	if err != nil {
		return nil, err
	}
	return &_UnReadInfoDao{mc: mc}, nil
}

func (db *_UnReadInfoDao) Insert(ctx context.Context, dbItem *UnReadInfoDbItem) error {
	dbItem.UnReadId = db.genUnReadId(dbItem.ProjectId, dbItem.MemberUserId, dbItem.BulletinId)
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_UnReadInfoDao) genUnReadId(projectId, memberUserId, bulletinId string) string {
	return fmt.Sprintf("%s:%s:%s", projectId, memberUserId, bulletinId)
}

func (db *_UnReadInfoDao) Exist(ctx context.Context, projectId, memberUserId, bulletinId string) (bool, error) {
	unReadId := db.genUnReadId(projectId, memberUserId, bulletinId)
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": unReadId, "projectId": projectId})
	return count > 0, err
}

func (db *_UnReadInfoDao) Remove(ctx context.Context, projectId, memberUserId, bulletinId string) error {
	unReadId := db.genUnReadId(projectId, memberUserId, bulletinId)
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": unReadId, "projectId": projectId})
	return err
}

func (db *_UnReadInfoDao) RemoveByMember(ctx context.Context, projectId, memberUserId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"projectId": projectId, "memberUserId": memberUserId})
	return err
}

func (db *_UnReadInfoDao) RemoveByBulletin(ctx context.Context, projectId, bulletinId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"projectId": projectId, "bulletinId": bulletinId})
	return err
}

func (db *_UnReadInfoDao) Count(ctx context.Context, projectId, memberUserId string) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"projectId": projectId, "memberUserId": memberUserId})
	return uint32(count), err
}

func (db *_UnReadInfoDao) List(ctx context.Context, projectId, memberUserId string, offset, limit int64) ([]*UnReadInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"projectId": projectId, "memberUserId": memberUserId},
		options.Find().SetSkip(offset).SetLimit(limit).SetSort(bson.M{"timeStamp": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*UnReadInfoDbItem{}
	err = cursor.All(ctx, &retList)
	if err != nil {
		return nil, err
	}
	return retList, nil
}

func (db *_UnReadInfoDao) DistinctById(ctx context.Context, projectId, memberUserId string, bulletinIdList []string) ([]string, error) {
	tmpItemList, err := db.mc.Distinct(ctx, "bulletinId",
		bson.M{"projectId": projectId, "memberUserId": memberUserId, "bulletinId": bson.M{"$in": bulletinIdList}})
	if err != nil {
		return nil, err
	}
	retList := []string{}
	for _, tmpItem := range tmpItemList {
		retList = append(retList, tmpItem.(string))
	}
	return retList, nil
}
